list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(f"list: {list}")

# 列表的切片
print(f"list[1:3]={list[1:3]}")

"""
# 带步长的切片 list[start:end:step]
print(f"list[1:4:2]={list[1::2]}")

print(f"list[:4]={list[:4]}")  # 0,1,2,3

print(f"list[4:]={list[4:]}")  # 4,5,6,7,8,9

print(f"list[::2]={list[::2]}")  # 0,2,4,6,8

print(f"list[::-1]={list[::-1]}")  # 9,8,7,6,5,4,3,2,1,0

new_list = list[::-1]
print(f"new_list: {new_list},old_list: {list}")

new_list_1 = list[1:4]  # 创建新列表，不会影响原列表
print(f"new_list_1: {new_list_1},new_list_1d: {id(new_list_1)}")

new_list_1.append(10)
print(f"new_list_1: {new_list_1},new_list_1d: {id(new_list_1)}")



a = [1, 2]
a.append([3, 4])
print(a)  # [1, 2, [3, 4]]  # 整个 [3, 4] 作为一个元素

a.extend([5, 6])
print(a)  # [1, 2, [3, 4], 5, 6]  # 5 和 6 分别添加进列表



x = [1, 2, 3, 4, 5, 3]
x.remove(3)
print(x)  # [1, 2, 4, 5]

for i in range(len(x)):
    y = x.pop()
    print(f"y={y},x={x}")





list1 = [1, 2, 3]
list2 = [4, 5, 6]

combined = list1 + list2
print(combined)
print(f"combined: {combined},list1={list1},list2={list2}")



fruits = ["apple", "banana", "orange"]

vegetables = ["carrot", "broccoli", "spinach"]

hybird = fruits + vegetables

print(f"hybird: {hybird}")
for index, item in enumerate(hybird):
    print(f"item={item},index={index}")
    for i in range(len(hybird)):
        if i <= len(fruits) - 1:
            _fruit = fruits[i]
            if _fruit in hybird:
                print(f"fruits[i]={fruits[i]}")
        if i <= len(vegetables) - 1:
            _vegetable = vegetables[i - len(fruits)]
            if _vegetable in hybird:
                print(f"vegetables[i]={vegetables[i]}")


x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 2, 4, 7]

_count_1 = x.count(1)
print(f"_count_1={_count_1}")


# 列表的排序

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 2, 4, 7]

y = x.copy()  # 拷贝是在内存中重新创建一个对象，执行后，y和x的id不同
print(f"y={y},y_id={id(y)},x_id={id(x)}")

z = x.sort()  # 排序是在原列表上进行操作，执行后，z和x的id相同
print(f"z={z},z_id={id(z)},x_id={id(x)},x={x}")

x.sort(reverse=True)
print(f"x={x}")



# 列表的推导式 新列表 = [表达式 for 变量 in 可迭代对象 if 条件]

ages = [1, 10, 20, 30, 40, 50, 60]
new_ages = [age + 20 for age in ages]
print(f"推到出来的新数组: {new_ages}")

# 筛选出年龄大于等于30的人
adults = [age if age > 30 else age + 100 for age in ages]
print(f"adults: {adults}")


nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
new_old_nums = [num if num % 2 == 0 else num * 2 for num in nums]
print(f"new_old_nums: {new_old_nums}")


# 打印二维数组
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

for row in matrix:
    for item in row:
        print(item, end=" ")
    print()



# 深拷贝和浅拷贝
# 浅拷贝
# 1 , 2
# 3 , 4
#
a = [[1, 2], [3, 4]]
b = a.copy()  # 只复制最外层
print(f"b={b},b_id={id(b)},a_id={id(a)}")
b[0][0] = 99
print(f"b={b},a={a}")  # [[99, 2], [3, 4]]，原列表被影响

# 深拷贝
import copy

a = [[1, 2], [3, 4]]
b = copy.deepcopy(a)
b[0][0] = 88
print(a)  # [[1, 2], [3, 4]]，原列表不变
print(b)  # [[88, 2], [3, 4]]

"""

# del 根据索引删除元素

x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(f"x={x}")
del x[0]  # 删除索引为0的元素 [1,2,3,4,5,6,7,8,9]
print(f"x={x}")

# 根据切片删除元素,切片都是左闭右开区间
y = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(f"y={y}")
del y[1:3]  # 删除索引为1,2的元素 [1,4,5,6,7,8,9]
print(f"y={y}")
